package com.polaris.live.ui.party.widget

import android.content.Context
import android.util.AttributeSet
import android.view.GestureDetector
import android.view.MotionEvent
import android.widget.FrameLayout
import com.polaris.live.common.ext.dp
import kotlin.math.abs

/**
 * 左右滑动监听
 * @author Created by 天晴 on 2024/2/26 19:35
 * @since 1.0.0
 **/
class GestureView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
    private var dx = 0f
    private var dy = 0f
    var time = 0L
    private var touch = false
    private var opentouch = true
    private var left: () -> Unit = {}
    private var right: () -> Unit = {}
    private var up: () -> Unit = {}
    private var down: () -> Unit = {}
    private val gestureDetector: GestureDetector


    init {
        gestureDetector = GestureDetector(context, GestureListener())
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (opentouch) {
            var pd = false
            when (ev.action) {
                MotionEvent.ACTION_DOWN -> {
                    dx = ev.x
                    dy = ev.y
                    time = System.currentTimeMillis()
                }

                MotionEvent.ACTION_MOVE -> {
                    val deltaX = dx.minus(ev.x)
                    val deltaY = dy.minus(ev.y)
                    if (abs(deltaX) > abs(deltaY)) {
                        pd = true
                    }
                }

                MotionEvent.ACTION_UP -> {
                    if (System.currentTimeMillis() - time < THRESHOLD) {
                        val deltaX = dx.minus(ev.x)
                        val deltaY = dy.minus(ev.y)
                        if (abs(deltaX) > abs(deltaY) && abs(deltaX) > snapMinDistance) {
                            if (deltaX > 0) {
                                // 右滑逻辑
                                onSwipeRight()
                            } else {
                                // 左滑逻辑
                                onSwipeLeft()
                            }
                            pd = true
                        }
                    }
                    dx = 0f
                    dy = 0f
                    time = 0
                }
            }
            return pd || super.dispatchTouchEvent(ev)

        } else {
            return super.dispatchTouchEvent(ev)
        }

    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return if (opentouch) {
            if (touch) {
                gestureDetector.onTouchEvent(event)
            }
            touch
        } else {
            super.onTouchEvent(event)
        }

    }

    /**
     * 是否开启手势
     */
    fun setOpenTouch(touch: Boolean) {
        this.opentouch = touch
    }

    /**
     * 自己处理事件
     */
    fun setTouch(touch: Boolean) {
        this.touch = touch
    }

    /**
     * 设置左滑优化监听
     */
    fun setOnListener(left: () -> Unit, right: () -> Unit, up: () -> Unit, down: () -> Unit) {
        this.left = left
        this.right = right
        this.up = up
        this.down = down
    }

    private fun onSwipeLeft() {
        left.invoke()
    }

    private fun onSwipeRight() {
        right.invoke()
    }

    private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
        override fun onFling(e1: MotionEvent?, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
            val distanceX = e2.x - (e1?.x ?: 0f)
            val distanceY = e2.y - (e1?.y ?: 0f)
            if (abs(distanceX) < abs(distanceY)) {
                // 纵向滑动
                if (distanceY > 0) {
                    down.invoke()
                } else {
                    up.invoke()
                }
            }
            return true
        }
    }

    companion object {
        private const val THRESHOLD = 200 //快速滑动时间阈值
        private val snapMinDistance = 25.dp //最小被判定为移动的距离
    }

}